	package com.achievo.know.web.component;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.FileApplication;
import com.achievo.know.component.impl.FileManagerConfig;
import com.achievo.know.core.ExtendedPropertyPlaceholderConfigurer;
import com.achievo.know.domain.FileAttachment;
import com.achievo.know.domain.TempAttachment;
import com.achievo.know.web.util.BaseActionSupport;
import com.redhat.redwork.coc.RedworkAction;

public class UploadAction extends BaseActionSupport implements RedworkAction {

	private File[] uploadify;

	private String[] uploadifyFileName; // 上传来的文件的名字
	
	private String tempFileId;
	
	private String ic;
	
	private String imgType;
	
	@Autowired
	private FileApplication fileApplication;
	@Autowired
    private FileManagerConfig config;
	@Autowired
	private ExtendedPropertyPlaceholderConfigurer configHolder;

	/**
	 * 
	 */
	private static final long serialVersionUID = -5247245013618740662L;

	public void upload() throws IOException {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8"); // 务必，防止返回文件名是乱码
		
		int i=0;
		for(String filename:uploadifyFileName){
			//FileAttachment f=fileApplication.getFileByName(filename);
			/*if(f!=null && !"image".equals(ic)){
				continue;
			}*/
			TempAttachment temp=new TempAttachment();// 保存文件到上传目录
			if(tempFileId.indexOf("-")>=0){
				imgType=tempFileId.split("-")[1];
				tempFileId=tempFileId.split("-")[0];
			}
			temp.setTempFileId(tempFileId);
			temp.setAttachName(FilenameUtils.getBaseName(filename));
			temp.setCheckSum(FileUtils.checksumCRC32(uploadify[i]));
			temp.setAttachSize((int)(uploadify[i].length()/1000));
			temp.setAttachType(FilenameUtils.getExtension(filename));
			temp.setGuid(config.getNameGenerator().newFileName());
			temp.setAttachPath(config.getNameGenerator().generateDirectoryPath());
			if("photo".equals(imgType)){
				FileAttachment aaa=fileApplication.getAttachmentByGuid(tempFileId);
				if(aaa!=null){
					temp.setAttachPath(aaa.getAttachPath());
				}
			}
			temp.setCanDown(true);
			temp.setCanFaq(true);
			fileApplication.addTempAttachment(temp);
			File parent=new File(config.getUploadFileStoreDirectory()+java.io.File.separator+temp.getAttachPath());
			if(!parent.exists())
				parent.mkdirs();
			uploadify[i++].renameTo(new File(parent,temp.getGuid()+"."+temp.getAttachType() )); //保存文件 
		}

		response.getWriter().write("ok");
		//return null; // 这里不需要页面转向，所以返回空就可以了
	}
	
	
	
	public File[] getUploadify() {
		return uploadify;
	}

	public void setUploadify(File[] uploadify) {
		this.uploadify = uploadify;
	}

	public String[] getUploadifyFileName() {
		return uploadifyFileName;
	}

	public void setUploadifyFileName(String[] uploadifyFileName) {
		this.uploadifyFileName = uploadifyFileName;
	}


	public String getTempFileId() {
		return tempFileId;
	}

	public void setTempFileId(String tempFileId) {
		this.tempFileId = tempFileId;
	}

	public FileApplication getFileApplication() {
		return fileApplication;
	}

	public void setFileApplication(FileApplication fileApplication) {
		this.fileApplication = fileApplication;
	}

	public FileManagerConfig getConfig() {
		return config;
	}

	public void setConfig(FileManagerConfig config) {
		this.config = config;
	}

	public ExtendedPropertyPlaceholderConfigurer getConfigHolder() {
		return configHolder;
	}

	public void setConfigHolder(ExtendedPropertyPlaceholderConfigurer configHolder) {
		this.configHolder = configHolder;
	}



	public String getIc() {
		return ic;
	}



	public void setIc(String ic) {
		this.ic = ic;
	}



	public String getImgType() {
		return imgType;
	}



	public void setImgType(String imgType) {
		this.imgType = imgType;
	}

	

}
